/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.ui.dialog.calendar;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.common.MultiThemeUtils;
import com.ecarx.systemui.plugin.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class CalendarEnAdapter extends RecyclerView.Adapter<CalendarEnAdapter.CalendarViewHolder> {

    private final List<CalendarEnData.CalendarItemEntity> datas = new ArrayList<>();
    private Context context;
    private String xcTheme;

    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<CalendarEnData.CalendarItemEntity> datas) {
        this.datas.clear();
        this.datas.addAll(datas);
        this.notifyDataSetChanged();
    }

    @NonNull
    @Override
    public CalendarViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        context = viewGroup.getContext();
        if(TextUtils.isEmpty(xcTheme)){
            xcTheme = Settings.Global.getString(context.getContentResolver(), "XCTheme");
        }
        if(TextUtils.isEmpty(xcTheme)){
            xcTheme = "Orange";
        }
        LogUtils.d("CalendarEnAdapter-Tag", "xcTheme:" + xcTheme);
        View view = LayoutInflater.from(context).inflate(R.layout.item_en_calendar_dialog, null, false);
        return new CalendarEnAdapter.CalendarViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CalendarViewHolder calendarViewHolder, int position) {
        CalendarEnData.CalendarItemEntity calendarItemEntity = datas.get(position);
        int color;
        if (calendarItemEntity.getIsLastOrNext() == 0) {
            if(calendarItemEntity.isCurrentDay()){
                switch (xcTheme) {
                    case "Pink":
                        color = context.getColor(R.color.text_theme_pink);
                        break;
                    case "Red":
                        color = context.getColor(R.color.text_theme_red);
                        break;
                    case "Yellow":
                        color = context.getColor(R.color.text_theme_yellow);
                        break;
                    case "Edition1":
                        color = context.getColor(R.color.text_theme_edition1);
                        break;
                    case "Blue":
                        color = context.getColor(R.color.text_theme_blue);
                        break;
                    default:
                        color = context.getColor(R.color.text_theme_orange);
                        break;
                }
            }else{
                color = context.getColor(R.color.calendar_dialog_text1_color);
            }
        } else {
            color = context.getColor(R.color.calendar_dialog_text2_color);
        }
        calendarViewHolder.tvYlDay.setTextColor(color);
        if (calendarItemEntity.isCurrentDay()) {
            calendarViewHolder.viewCurtDay.setVisibility(View.VISIBLE);
            int resourceIdByTheme = MultiThemeUtils.getResourceIdByTheme(context, R.drawable.dialog_calendar_curt_day);
            calendarViewHolder.viewCurtDay.setBackgroundResource(resourceIdByTheme);
        } else {
            calendarViewHolder.viewCurtDay.setVisibility(View.INVISIBLE);
            calendarViewHolder.viewCurtDay.setBackgroundColor(Color.TRANSPARENT);
        }
        calendarViewHolder.tvYlDay.setText(String.valueOf(calendarItemEntity.getYlDay()));
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public static class CalendarViewHolder extends RecyclerView.ViewHolder {

        TextView tvYlDay;
        View viewCurtDay;

        public CalendarViewHolder(@NonNull View itemView) {
            super(itemView);
            tvYlDay = itemView.findViewById(R.id.tv_yl_day);
            viewCurtDay = itemView.findViewById(R.id.view_curt_day);
        }
    }
}
